package com.liukai.bishe.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.liukai.bishe.R;
import com.liukai.bishe.model.doman.transaction.TransactionRankList;
import com.liukai.bishe.utils.DateUtils;
import com.robinhood.ticker.TickerUtils;
import com.robinhood.ticker.TickerView;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

public class RankListAdapter extends RecyclerView.Adapter<RankListAdapter.RankListViewHolder> {

    private Context mContext;
    private List<TransactionRankList> mTransactionRankLists;


    public RankListAdapter(List<TransactionRankList> transactionRankListList, Context context) {

        this.mContext = context;
        this.mTransactionRankLists = transactionRankListList;

    }

    @NonNull
    @Override
    public RankListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = View.inflate(mContext,R.layout.item_recyclerview_ranklist,null);

        return new RankListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RankListViewHolder holder, int position) {

        TransactionRankList transactionRankList = mTransactionRankLists.get(position);
        String name = transactionRankList.getNickname();
        if (name == null){
            name = transactionRankList.getUsername();
        }
        String describe = transactionRankList.getDescribe();
        String userHead = transactionRankList.getUserhead();
        String tvTalCount = transactionRankList.getAllcount();
        String tvTotalTime = transactionRankList.getAlltime();
        String time = DateUtils.timeMillisToTime("HH:mm",tvTotalTime);
        String[] split = time.split(":");
        String tvHours = split[0];
        String tvMinutes = split[1];

        holder.tvName.setText(name);
        holder.tvRanking.setText(position + 1 + "");
        if (userHead != null){
            Glide.with(mContext).load(userHead).into(holder.userHeadImg);
        }
        if (describe != null){
            holder.tvDescribe.setText(describe);
        }
        if (tvHours != null) {
            holder.tvTotalTimeHours.setText(tvHours);
        }
        if (tvMinutes != null) {
            holder.tvTotalTimeMinutes.setText(tvMinutes);
        }
        if (tvTalCount != null) {
            holder.tvTotalCount.setText("今日专注"+tvTalCount+"次");
        }

        if (position == 0){
            holder.tvRanking.setTextColor(mContext.getColor(R.color.rankOne));
        }else if (position == 1){
            holder.tvRanking.setTextColor(mContext.getColor(R.color.rankTwo));
        }else if (position == 2){
            holder.tvRanking.setTextColor(mContext.getColor(R.color.rankThree));
        }


    }

    @Override
    public int getItemCount() {
        return mTransactionRankLists.size();
    }

    class RankListViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.ranking)
        public TextView tvRanking;

        @BindView(R.id.userHead)
        public CircleImageView userHeadImg;

        @BindView(R.id.tvName)
        public TextView tvName;

        @BindView(R.id.describe)
        public TextView tvDescribe;

        @BindView(R.id.tvTotalCount)
        public TextView tvTotalCount;

        @BindView(R.id.tvTotalTimeMinutes)
        public TickerView tvTotalTimeMinutes;

        @BindView(R.id.tvTotalTimeHours)
        public TickerView tvTotalTimeHours;


        public RankListViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);

            tvTotalTimeHours.setCharacterLists(TickerUtils.provideNumberList());
            tvTotalTimeMinutes.setCharacterLists(TickerUtils.provideNumberList());


        }
    }

}
